package com.itheima.interceptor;

import com.itheima.until.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String url = request.getRequestURL().toString();
//        if (url.contains("/login")){
//            log.info("登录请求，放行");
//            return true;
//        }
        String token = request.getHeader("token");
        if (token==null||token.length()==0){
            log.info("请求头中未携带token");
            response.setStatus(401);
            return false;
        }
        try {
            JwtUtils.parseJWT(token);
        }catch (Exception e){
            log.info("解析错误");
            response.setStatus(401);
            return false;
        }
        log.info("token校验通过");
        return true;
    }
}
